<template>
  <el-card shadow="hover">
    <b>儿童散学归来早，忙趁东风放纸鸢</b>
  </el-card>

  <el-card style="margin-top: 20px">
    <el-upload
        ref="upload"
        class="upload-demo"
        action="http://localhost:3300/userInfo"
        :limit="1"
        :on-exceed="handleExceed"
        :auto-upload="false"
    >
      <template #trigger>
        <el-button type="primary">select file</el-button>
      </template>
      <el-button class="ml-3" type="success" @click="submitUpload">
        upload to server
      </el-button>
      <template #tip>
        <div class="el-upload__tip text-red">
          limit 1 file, new file will cover the old file
        </div>
      </template>
    </el-upload>
  </el-card>

  <el-card style="margin-top: 20px">
    <el-button>最新</el-button>
  </el-card>

</template>

<script lang="ts" setup>

import {getCurrentInstance, ref} from 'vue'
import { genFileId } from 'element-plus'
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'

const upload = ref<UploadInstance>()

const handleExceed: UploadProps['onExceed'] = (files) => {
  upload.value!.clearFiles()
  const file = files[0] as UploadRawFile
  file.uid = genFileId()
  upload.value!.handleStart(file)
}

const submitUpload = () => {
  upload.value!.submit()
}

</script>

<style>
.el-card{
  text-align: center;
}

</style>
